package com.mali.malibackendproduct.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mali.backendcommon.common.Result;
import com.mali.backendcommon.constant.AuthenticationConstant;
import com.mali.backendcommon.util.UserContext;
import com.mali.malibackendmodel.dto.BuyWayDTO;
import com.mali.malibackendmodel.dto.SellProductDTO;
import com.mali.malibackendmodel.dto.TopProductsDTO;
import com.mali.malibackendmodel.pojo.Orders;
import com.mali.malibackendproduct.mapper.OrdersMapper;
import com.mali.malibackendproduct.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.List;

/**
* @author 132993
* @description 针对表【orders(订单)】的数据库操作Service实现
* @createDate 2024-06-16 10:20:11
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 根据商品id查询商品在过去指定时间范围内中每天的兑换量
     * @param beginTime 起始时间
     * @param endTime   结束时间
     * @return
     */
    @Override
    public Result<List<SellProductDTO>> getProductExchange(LocalDate beginTime, LocalDate endTime) {
        if(UserContext.getUser().getRole() != AuthenticationConstant.SUPER_ADMIN) {
            return Result.error("抱歉，您暂无权限操作");
        }
        //构建默认值
        if(beginTime == null) {
            beginTime = LocalDate.now().plusDays(-8);
        }
        if (endTime == null) {
            endTime = LocalDate.now().plusDays(-1);
        }
        endTime = endTime.plusDays(1);
        //校验参数合法性
        if(beginTime.isAfter(endTime) || beginTime.plusDays(60).isBefore(endTime) || endTime.isAfter(LocalDate.now())) {
            return Result.error("参数不合法");
        }

        //访问数据库
        List<SellProductDTO> sellProductsList = ordersMapper.selectProductExchange(beginTime, endTime);
        if (sellProductsList.isEmpty()) {
            return Result.success();
        }else {
            return Result.success(sellProductsList);
        }
    }

    @Override
    public Result productsTop(LocalDate beginTime, LocalDate endTime) {
        if(UserContext.getUser().getRole() != AuthenticationConstant.SUPER_ADMIN) {
            return Result.error("抱歉，您暂无权限操作");
        }
        //构建默认值
        if(beginTime == null) {
            beginTime = LocalDate.now().plusDays(-6);
        }
        if (endTime == null) {
            endTime = LocalDate.now().plusDays(-1);
        }
        endTime = endTime.plusDays(1);
        //校验参数合法性
        if(beginTime.isAfter(endTime) || beginTime.plusDays(60).isBefore(endTime) || endTime.isAfter(LocalDate.now())) {
            return Result.error("参数不合法");
        }

        //访问数据库
        List<TopProductsDTO> top20List = ordersMapper.selectTopProducts(beginTime, endTime);
        if (top20List.isEmpty()) {
            return Result.success();
        }else {
            return Result.success(top20List);
        }
    }

    @Override
    public Result buyWay(LocalDate beginTime, LocalDate endTime) {
        if(UserContext.getUser().getRole() != AuthenticationConstant.SUPER_ADMIN) {
            return Result.error("抱歉，您暂无权限操作");
        }
        //构建默认值
        if(beginTime == null) {
            beginTime = LocalDate.now().plusDays(-6);
        }
        if (endTime == null) {
            endTime = LocalDate.now().plusDays(-1);
        }
        endTime = endTime.plusDays(1);
        //校验参数合法性
        if(beginTime.isAfter(endTime) || beginTime.plusDays(60).isBefore(endTime) || endTime.isAfter(LocalDate.now())) {
            return Result.error("参数不合法");
        }

        //访问数据库
        List<BuyWayDTO> buyWayDTOList = ordersMapper.selectBuyWay(beginTime, endTime);

        //将每种兑换方式的兑换数量结果变成占比存储
        double sum = 0;
        for (BuyWayDTO buyWayDTO : buyWayDTOList) {
            sum += buyWayDTO.getNum();
        }
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        for (BuyWayDTO buyWayDTO : buyWayDTOList) {
            double num = buyWayDTO.getNum();
            decimalFormat.setRoundingMode(RoundingMode.HALF_UP); // 设置舍入模式
            buyWayDTO.setNum(Double.parseDouble(decimalFormat.format(num / sum * 100)));
        }

        return Result.success(buyWayDTOList);
    }


}




